package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlLqhlkljpjlbBsDetail;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlLqhlkljpjlbBsDetailVo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlLqhlkljpjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlLqhlkljpjlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlLqhlkljpjlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlLqhlkljpjlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlLqhlkljpjlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlLqhlkljpjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料沥青含量、矿料级配试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-08-31
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlLqhlkljpjlbBsInfoServiceImpl implements IReportLqhhlLqhlkljpjlbBsInfoService {

    private final ReportLqhhlLqhlkljpjlbBsInfoMapper baseMapper;
    private final ReportLqhhlLqhlkljpjlbBsDetailMapper detailMapper;

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlLqhlkljpjlbBsInfoVo queryById(Long excelId) {
        ReportLqhhlLqhlkljpjlbBsInfoVo reportLqhhlLqhlkljpjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlLqhlkljpjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsDetail>().eq(ReportLqhhlLqhlkljpjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            reportLqhhlLqhlkljpjlbBsInfoVo.setDetailList(detailList);
        }
        return reportLqhhlLqhlkljpjlbBsInfoVo;

    }

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqhhlLqhlkljpjlbBsInfoVo> queryPageList(ReportLqhhlLqhlkljpjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlLqhlkljpjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录列表
     */
    @Override
    public List<ReportLqhhlLqhlkljpjlbBsInfoVo> queryList(ReportLqhhlLqhlkljpjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsInfo> buildQueryWrapper(ReportLqhhlLqhlkljpjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlLqhlkljpjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlLqhlkljpjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlLqhlkljpjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlLqhlkljpjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlLqhlkljpjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlLqhlkljpjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlLqhlkljpjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlLqhlkljpjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlLqhlkljpjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlLqhlkljpjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlLqhlkljpjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlLqhlkljpjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlLqhlkljpjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlLqhlkljpjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlLqhlkljpjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlLqhlkljpjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlLqhlkljpjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegcc()), ReportLqhhlLqhlkljpjlbBsInfo::getJiegcc, bo.getJiegcc());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhllx()), ReportLqhhlLqhlkljpjlbBsInfo::getHunhllx, bo.getHunhllx());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqzlbh()), ReportLqhhlLqhlkljpjlbBsInfo::getLiqzlbh, bo.getLiqzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getZuijlqyl()), ReportLqhhlLqhlkljpjlbBsInfo::getZuijlqyl, bo.getZuijlqyl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiplx()), ReportLqhhlLqhlkljpjlbBsInfo::getJiplx, bo.getJiplx());
        lqw.eq(bo.getGanzsyzl1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getGanzsyzl1, bo.getGanzsyzl1());
        lqw.eq(bo.getGanzsyzl2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getGanzsyzl2, bo.getGanzsyzl2());
        lqw.eq(bo.getShuixhsszl1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getShuixhsszl1, bo.getShuixhsszl1());
        lqw.eq(bo.getShuixhsszl2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getShuixhsszl2, bo.getShuixhsszl2());
        lqw.eq(bo.getShuixhsxl1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getShuixhsxl1, bo.getShuixhsxl1());
        lqw.eq(bo.getShuixhsxl2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getShuixhsxl2, bo.getShuixhsxl2());
        lqw.eq(bo.getTonggl1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getTonggl1, bo.getTonggl1());
        lqw.eq(bo.getTonggl2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getTonggl2, bo.getTonggl2());
        lqw.eq(bo.getPingj() != null, ReportLqhhlLqhlkljpjlbBsInfo::getPingj, bo.getPingj());
        lqw.eq(bo.getM11() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM11, bo.getM11());
        lqw.eq(bo.getM12() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM12, bo.getM12());
        lqw.eq(bo.getM21() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM21, bo.getM21());
        lqw.eq(bo.getM22() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM22, bo.getM22());
        lqw.eq(bo.getM31() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM31, bo.getM31());
        lqw.eq(bo.getM32() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM32, bo.getM32());
        lqw.eq(bo.getM41() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM41, bo.getM41());
        lqw.eq(bo.getM42() != null, ReportLqhhlLqhlkljpjlbBsInfo::getM42, bo.getM42());
        lqw.eq(bo.getBiaodzlssxs1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getBiaodzlssxs1, bo.getBiaodzlssxs1());
        lqw.eq(bo.getBiaodzlssxs2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getBiaodzlssxs2, bo.getBiaodzlssxs2());
        lqw.eq(bo.getLiqyl1() != null, ReportLqhhlLqhlkljpjlbBsInfo::getLiqyl1, bo.getLiqyl1());
        lqw.eq(bo.getLiqyl2() != null, ReportLqhhlLqhlkljpjlbBsInfo::getLiqyl2, bo.getLiqyl2());
        lqw.eq(bo.getPingjz() != null, ReportLqhhlLqhlkljpjlbBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlLqhlkljpjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlLqhlkljpjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlLqhlkljpjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlLqhlkljpjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlLqhlkljpjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlLqhlkljpjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlLqhlkljpjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlLqhlkljpjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料沥青含量、矿料级配试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlLqhlkljpjlbBsInfoBo bo) {
        ReportLqhhlLqhlkljpjlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlLqhlkljpjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlLqhlkljpjlbBsDetail> details = new ArrayList<>(14);
            for (int i = 1; i <= 14; i++) {
                details.add(new ReportLqhhlLqhlkljpjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料沥青含量、矿料级配试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlLqhlkljpjlbBsInfoBo bo) {
        ReportLqhhlLqhlkljpjlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlLqhlkljpjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqhhlLqhlkljpjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlLqhlkljpjlbBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料沥青含量、矿料级配试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
